package com.sxm.yiwei.adapters.pinche;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.beans.pinche.Order;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.pinche.ReleaseActivity;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.CallPhone;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.views.CircleImage;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/21.
 */

public class OrderInProcessAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context context;
    private ArrayList<Order> list;
    private OrderInProcessAdapter.OnOrderHandleListener onOrderHandleListener;
    public OrderInProcessAdapter(Context context,ArrayList<Order> list) {
        this.context=context;
        this.list=list;
        inflater = LayoutInflater.from(context);

    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = 0;
        Order order = list.get(position);
        if (order.getIdentity()==Constant.PASSENGER){
            switch (order.getOrder_type()){
                case Constant.WAIT_PAY:
                    viewType=0;//乘客待付款
                    break;
                case Constant.WAIT_TAKE:
                    viewType=1;//乘客待接单
                    break;
                case Constant.WAIT_SURVICE:
                case Constant.IN_SURVICE:
                case Constant.WAIT_FINISH:
                    viewType=2;//乘客剩余
                    break;

            }
        }else {
            switch (order.getOrder_type()){
                case Constant.WAIT_PAY:
                    viewType=3;//车主待付款
                    break;
                case Constant.WAIT_TAKE:
                    viewType=4;//车主待接单
                    break;
                case Constant.WAIT_SURVICE:
                case Constant.IN_SURVICE:
                case Constant.WAIT_FINISH:
                    viewType=5;//车主剩余
                    break;

            }
        }
        return viewType;

    }
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 6;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final Order order = list.get(i);
        if (getItemViewType(i)==0||getItemViewType(i)==1||getItemViewType(i)==2){//乘客身份
            ViewHolder holder=null;
            if (view==null){
                holder=new ViewHolder();
                if (getItemViewType(i)==0){//乘客待付款
                    view=inflater.inflate(R.layout.passenger_wait_pay_long_item,null);
                    holder.wp_title_info =(TextView)view.findViewById(R.id.tv_title_info);
                    holder.wp_title_money =(TextView)view.findViewById(R.id.tv_title_money);
                    holder.wp_icon =(CircleImage) view.findViewById(R.id.iv_icon);
                    holder.wp_nickname =(TextView)view.findViewById(R.id.tv_nickname);
                    holder.wp_time =(TextView)view.findViewById(R.id.tv_recent_come);
                    holder.wp_words =(TextView)view.findViewById(R.id.tv_words);
                    holder.wp_start_pos =(TextView)view.findViewById(R.id.tv_start_pos);
                    holder.wp_end_pos =(TextView)view.findViewById(R.id.tv_end_pos);
                    holder.wp_seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
                    holder.wp_total_price =(TextView)view.findViewById(R.id.tv_total_price);
                    holder.wp_departTime =(TextView)view.findViewById(R.id.tv_depart_time);
                    holder.wp_btnBuy =(TextView)view.findViewById(R.id.btn_buy);
                    holder.wp_line =(View)view.findViewById(R.id.title_line);
                    holder.wp_tv_words_release =(TextView)view.findViewById(R.id.tv_words_release);
                    holder.wp_ll_buy =(LinearLayout)view.findViewById(R.id.ll_buy);
                    holder.wp_ll_bottom_release =(LinearLayout)view.findViewById(R.id.ll_bottom_release);
                    holder.wp_ll_bottom_buy =(LinearLayout)view.findViewById(R.id.ll_bottom_buy);
                    holder.wp_release_again =(TextView)view.findViewById(R.id.btn_release_again);
                }else if (getItemViewType(i)==1){//乘客待接单
                    view=inflater.inflate(R.layout.passenger_wait_take_long_item,null);
                    holder.wt_title_info =(TextView)view.findViewById(R.id.tv_title_info);
                    holder.wt_words =(TextView)view.findViewById(R.id.tv_words);
                    holder.wt_start_pos =(TextView)view.findViewById(R.id.tv_start_pos);
                    holder.wt_end_pos =(TextView)view.findViewById(R.id.tv_end_pos);
                    holder.wt_seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
                    holder.wt_total_price =(TextView)view.findViewById(R.id.tv_total_price);
                    holder.wt_departTime =(TextView)view.findViewById(R.id.tv_depart_time);
                    holder.wt_btnCancel =(TextView)view.findViewById(R.id.btn_phone);
                    holder.wt_btnReleaseAgain =(TextView)view.findViewById(R.id.btn_release_again);

                }else {//乘客剩余
                    view=inflater.inflate(R.layout.passenger_wait_survice_long_item,null);
                    holder.r_icon =(CircleImage) view.findViewById(R.id.iv_icon);
                    holder.r_nickname =(TextView)view.findViewById(R.id.tv_nickname);
                    holder.r_recent_come =(TextView)view.findViewById(R.id.tv_recent_come);
                    holder.r_words =(TextView)view.findViewById(R.id.tv_words);
                    holder.r_start_pos =(TextView)view.findViewById(R.id.tv_start_pos);
                    holder.r_end_pos =(TextView)view.findViewById(R.id.tv_end_pos);
                    holder.r_seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
                    holder.r_total_price =(TextView)view.findViewById(R.id.tv_total_price);
                    holder.r_loss =(TextView)view.findViewById(R.id.tv_loss_money);
                    holder.r_departTime =(TextView)view.findViewById(R.id.tv_depart_time);
                    holder.r_btnPhone =(TextView)view.findViewById(R.id.btn_phone);
                    holder.r_btnWei =(TextView)view.findViewById(R.id.btn_wei);
                    holder.r_text =(TextView)view.findViewById(R.id.text);

                    holder.r_line =(View)view.findViewById(R.id.line);
                    holder.r_title_info =(TextView)view.findViewById(R.id.tv_title_info);

                    holder.r_ll_loss =(LinearLayout)view.findViewById(R.id.ll_loss);
                    holder.r_btnInCar =(TextView)view.findViewById(R.id.btn_in_car);
                }
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
            if (getItemViewType(i)==0){//乘客待付款
                if (order.getForm_way()==Constant.RELEASE){//信息未发布
                    holder.wp_line.setVisibility(View.GONE);
                    holder.wp_tv_words_release.setVisibility(View.VISIBLE);
                    holder.wp_ll_buy.setVisibility(View.GONE);
                    holder.wp_ll_bottom_buy.setVisibility(View.GONE);
                    holder.wp_ll_bottom_release.setVisibility(View.VISIBLE);
                    if (TextUtils.isEmpty(order.getWords())){
                        holder.wp_tv_words_release.setText("我的留言：车主很懒，没有留言");
                    }else {
                        holder.wp_tv_words_release.setText("我的留言："+order.getWords());
                    }
                    if (order.getIs_timeout()==Constant.YES){
                        holder.wp_title_info.setText("已超时，需支付");
                        holder.wp_release_again.setText("重新发布");
                    }else {
                        holder.wp_title_info.setText("信息未发布，票价");
                        holder.wp_release_again.setText("立即发布");
                    }

                }else if (order.getForm_way()==Constant.BUY){//未购票
                    holder.wp_line.setVisibility(View.VISIBLE);
                    holder.wp_tv_words_release.setVisibility(View.GONE);
                    holder.wp_ll_buy.setVisibility(View.VISIBLE);
                    Picasso.with(context).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holder.wp_icon);
                    if(TextUtils.isEmpty(order.getNickname())){
                        holder.wp_nickname.setText("暂无昵称");
                    }else {
                        holder.wp_nickname.setText(order.getNickname());
                    }
                    if (order.getVehicle_type()==Constant.BUS){
                        holder.wp_nickname.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.mipmap.bus),null);
                    }else {
                        holder.wp_nickname.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.mipmap.car1),null);
                    }
                    if (TextUtils.isEmpty(order.getWords())){
                        holder.wp_words.setText("车主留言：车主很懒，没有留言");
                    }else {
                        holder.wp_words.setText("车主留言："+order.getWords());
                    }
                    holder.wp_time.setText(order.getStart_time());
                    if (order.getIs_timeout()==Constant.YES){
                        holder.wp_title_info.setText("已超时，需支付");
                        holder.wp_ll_bottom_release.setVisibility(View.GONE);
                        holder.wp_ll_bottom_buy.setVisibility(View.GONE);
                    }else {
                        holder.wp_title_info.setText("车票未购买成功，票价");
                        holder.wp_ll_bottom_buy.setVisibility(View.VISIBLE);
                        holder.wp_ll_bottom_release.setVisibility(View.GONE);
                    }
                }


                holder.wp_release_again.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Toast.makeText(context,"重新发布 "+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                        if (order.getIs_timeout()==Constant.YES){
                            Intent intent = new Intent(context, ReleaseActivity.class);
                            intent.putExtra("identity",order.getIdentity());
                            context.startActivity(intent);

                        }else {
                            //掉用生成订单接口，跳转支付页面
                            if (!TextUtils.isEmpty(order.getOrder_id())){
                                Intent intent = new Intent(context, BookPayActivity.class);
                                intent.putExtra("orderCode",order.getOrder_id());
                                intent.putExtra("amount", order.getTotal_money()+"");
                                intent.putExtra("from", Constant.PASSENGER_PUBLISH_PAY);
                                intent.putExtra("id",order.getService_id());
                                intent.putExtra("alipay",Common.PASSENGER_PUBLISH_ALIPAY);
                                intent.putExtra("yivipay",Common.PASSENGER_PUBLISH_RESTPAY);
                                context.startActivity(intent);
                            }else {
                                Toast.makeText(context,"order_id为空",Toast.LENGTH_SHORT).show();
                            }

                        }
                    }
                });
                holder.wp_btnBuy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Toast.makeText(context,"购票 "+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                        //先调用生成订单接口，在跳转支付页面
                        if (!TextUtils.isEmpty(order.getOrder_id())){
                            Intent intent = new Intent(context, BookPayActivity.class);
                            intent.putExtra("orderCode",order.getOrder_id());
                            intent.putExtra("amount", order.getTotal_money()+"");
                            intent.putExtra("from", Constant.PASSENGER_ROB_PAY);
                            intent.putExtra("id",order.getService_id());
                            intent.putExtra("alipay",Common.PASSENGER_SEARCH_ALIPAY);
                            intent.putExtra("yivipay",Common.PASSENGER_SEARCH_RESTPAY);
                            context.startActivity(intent);
                        }else {
                            Toast.makeText(context,"order_id为空",Toast.LENGTH_SHORT).show();
                        }


                    }
                });
                //公共
                holder.wp_start_pos.setText(order.getStart_place());
                holder.wp_end_pos.setText(order.getEnd_place());
                holder.wp_seats.setText(order.getSeats()+"个");
                holder.wp_total_price.setText(order.getTotal_money()+"元");
                holder.wp_departTime.setText(order.getDepart_time());
                holder.wp_title_money.setText(order.getSingle_money()+"元");
            }else if (getItemViewType(i)==1){//乘客待接单
                if (order.getIs_timeout()==Constant.YES){
                    holder.wt_title_info.setText("已超时");
                    holder.wt_btnReleaseAgain.setVisibility(View.VISIBLE);
                }else{
                    holder.wt_title_info.setText("待车主接单，请耐心等待");
                    holder.wt_btnReleaseAgain.setVisibility(View.GONE);
                }
                holder.wt_words.setText("我的留言："+order.getWords());
                holder.wt_start_pos.setText(order.getStart_place());
                holder.wt_end_pos.setText(order.getEnd_place());
                holder.wt_seats.setText(order.getSeats()+"个");
                holder.wt_total_price.setText(order.getTotal_money()+"元");
                holder.wt_departTime.setText(order.getDepart_time());
                holder.wt_btnReleaseAgain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Toast.makeText(context,"重新发布 "+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(context, ReleaseActivity.class);
                        intent.putExtra("identity",order.getIdentity());
                        context.startActivity(intent);
                        // TODO: 2017/11/14 费用是否退回
                    }
                });
                holder.wt_btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Toast.makeText(context,"取消订单 "+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                        //费用要返还
                        onOrderHandleListener.onCancelOrder(order.getForm_way()+"",order.getOrder_type()+"",order.getOrder_id()+"",i,Constant.CANCEL_ORDER);
                    }
                });
            }else {//乘客剩余
                Picasso.with(context).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holder.r_icon);
                if (TextUtils.isEmpty(order.getNickname())){
                    holder.r_nickname.setText("暂无昵称");

                }else {
                    holder.r_nickname.setText(order.getNickname());

                }
                holder.r_recent_come.setText("[24h前来过]");// TODO: 2017/11/14 喂一下
                if (order.getWho_created()==Constant.HOST){
                    holder.r_words.setText("我的留言："+order.getWords());
                    holder.r_words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chengke),null,null,null);
                    holder.r_text.setText("座位需求");
                }else {
                    holder.r_words.setText("车主留言："+order.getWords());
                    holder.r_words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chezhu),null,null,null);
                    holder.r_text.setText("提供座位");

                }

                holder.r_start_pos.setText(order.getStart_place());
                holder.r_end_pos.setText(order.getEnd_place());
                holder.r_seats.setText(order.getSeats()+"个");
                holder.r_total_price.setText(order.getTotal_money()+"元");
                holder.r_departTime.setText(order.getDepart_time());
                holder.r_loss.setText(order.getLoss_money()+"元");
                if (order.getOrder_type()==Constant.WAIT_SURVICE){
                    holder.r_ll_loss.setVisibility(View.VISIBLE);
                    holder.r_btnInCar.setVisibility(View.GONE);
                    holder.r_line.setVisibility(View.GONE);
                    holder.r_title_info.setVisibility(View.GONE);
                }else if (order.getOrder_type()==Constant.IN_SURVICE){
                    holder.r_ll_loss.setVisibility(View.GONE);
                    holder.r_btnInCar.setVisibility(View.VISIBLE);
                    holder.r_line.setVisibility(View.GONE);
                    holder.r_title_info.setVisibility(View.GONE);
                    if (order.getIs_in_car()==Constant.YES){
                        holder.r_btnInCar.setText("行程完结");

                    }else if (order.getIs_in_car()==Constant.NO){
                        holder.r_btnInCar.setText("我已上车");

                    }
                }else if (order.getOrder_type()==Constant.WAIT_FINISH){
                    holder.r_ll_loss.setVisibility(View.GONE);
                    if (order.getIs_finished()==Constant.HOST_FINISHED_PASSENGER_NO){
                        holder.r_btnInCar.setVisibility(View.VISIBLE);
                        holder.r_btnInCar.setText("行程完结");
                        holder.r_line.setVisibility(View.GONE);
                        holder.r_title_info.setVisibility(View.GONE);
                    }else if (order.getIs_finished()==Constant.PASSENGER_FINISHED_HOST_NO){
                        holder.r_btnInCar.setVisibility(View.GONE);
                        holder.r_line.setVisibility(View.VISIBLE);
                        holder.r_title_info.setVisibility(View.VISIBLE);
                        holder.r_title_info.setText("等待对方完结");
                    }

                }else if (order.getOrder_type()==Constant.FINISHED){
                    holder.r_ll_loss.setVisibility(View.GONE);
                    holder.r_btnInCar.setVisibility(View.VISIBLE);
                    holder.r_btnInCar.setText("再次发布");
                    holder.r_line.setVisibility(View.GONE);
                    holder.r_title_info.setVisibility(View.GONE);
                }

                holder.r_btnWei.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Toast.makeText(context,"喂一下 "+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                        Toast.makeText(context,"暂未开放",Toast.LENGTH_LONG).show();


                    }
                });
                holder.r_btnInCar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (order.getOrder_type()==Constant.FINISHED){
                            //Toast.makeText(context,"再次发布 "+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(context, ReleaseActivity.class);
                            intent.putExtra("identity",order.getIdentity());
                            context.startActivity(intent);

                        }else if (order.getOrder_type()==Constant.WAIT_FINISH){
                            //Toast.makeText(context,"行程完结 "+"身份"+identity+"订单类型"+order_type+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            onOrderHandleListener.onRouteFinish(order.getIdentity()+"",order.getOrder_id(),i,Constant.ROUTE_FINISHED);
                        }else if(order.getOrder_type()==Constant.IN_SURVICE){
                            if (order.getIs_in_car()==Constant.YES){
                                //Toast.makeText(context,"行程完结 "+"身份"+identity+"订单类型"+order_type+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                onOrderHandleListener.onRouteFinish(order.getIdentity()+"",order.getOrder_id(),i,Constant.ROUTE_FINISHED);

                            }else if (order.getIs_in_car()==Constant.NO){
                                //Toast.makeText(context,"我已上车 "+"身份"+identity+"订单类型"+order_type+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                onOrderHandleListener.onAlreadyInCarClick(order.getOrder_id(),i,Constant.ALREADY_IN_CAR);

                            }
                        }

                    }
                });
                holder.r_btnPhone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(order.getPhone())){
                            CallPhone.callPhone(context,order.getPhone());
                        }else {
                            Toast.makeText(context,"电话为空",Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }else {//车主身份
            ViewHolderHost holderHost=null;
            if (view==null){
                holderHost=new ViewHolderHost();
                if (getItemViewType(i)==3){//车主待付款
                    view=inflater.inflate(R.layout.host_wait_pay_long_item,null);
                    holderHost.wp_title_info =(TextView)view.findViewById(R.id.tv_title_info);
                    holderHost.wp_title_money =(TextView)view.findViewById(R.id.tv_title_money);
                    holderHost.wp_icon =(CircleImage) view.findViewById(R.id.iv_icon);
                    holderHost.wp_nickname =(TextView)view.findViewById(R.id.tv_nickname);
                    holderHost.wp_recent_come =(TextView)view.findViewById(R.id.tv_recent_come);
                    holderHost.wp_words =(TextView)view.findViewById(R.id.tv_words);
                    holderHost.wp_start_pos =(TextView)view.findViewById(R.id.tv_start_pos);
                    holderHost.wp_end_pos =(TextView)view.findViewById(R.id.tv_end_pos);
                    holderHost.wp_seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
                    holderHost.wp_total_price =(TextView)view.findViewById(R.id.tv_total_price);
                    holderHost.wp_departTime =(TextView)view.findViewById(R.id.tv_depart_time);
                    holderHost.wp_btn_release =(TextView) view.findViewById(R.id.btn_release);
                    holderHost.wp_ll_owner_info =(LinearLayout)view.findViewById(R.id.ll_owner_info);
                    holderHost.wp_line =(View)view.findViewById(R.id.title_line);
                    holderHost.wp_tv_words_release =(TextView)view.findViewById(R.id.tv_words_release);
                    holderHost.wp_loss =(TextView)view.findViewById(R.id.tv_loss);
                }else if (getItemViewType(i)==4){//车主待接单
                    view=inflater.inflate(R.layout.host_wait_take_long_item,null);
                    holderHost.wt_title_info =(TextView)view.findViewById(R.id.tv_title_info);
                    holderHost.wt_words =(TextView)view.findViewById(R.id.tv_words);
                    holderHost.wt_start_pos =(TextView)view.findViewById(R.id.tv_start_pos);
                    holderHost.wt_end_pos =(TextView)view.findViewById(R.id.tv_end_pos);
                    holderHost.wt_seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
                    holderHost.wt_total_price =(TextView)view.findViewById(R.id.tv_total_price);
                    holderHost.wt_loss =(TextView)view.findViewById(R.id.tv_loss_money);
                    holderHost.wt_price =(TextView)view.findViewById(R.id.tv_price);
                    holderHost.wt_departTime =(TextView)view.findViewById(R.id.tv_depart_time);
                    holderHost.wt_button1 =(TextView)view.findViewById(R.id.button1);
                    holderHost.wt_button2 =(TextView)view.findViewById(R.id.button2);
                }else {//车主剩余
                    view=inflater.inflate(R.layout.host_wait_survice_long_item,null);
                    holderHost.r_icon =(CircleImage) view.findViewById(R.id.iv_icon);
                    holderHost.r_nickname =(TextView)view.findViewById(R.id.tv_nickname);
                    holderHost.r_recent_come =(TextView)view.findViewById(R.id.tv_recent_come);
                    holderHost.r_words =(TextView)view.findViewById(R.id.tv_words);
                    holderHost.r_start_pos =(TextView)view.findViewById(R.id.tv_start_pos);
                    holderHost.r_end_pos =(TextView)view.findViewById(R.id.tv_end_pos);
                    holderHost.r_seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
                    holderHost.r_total_price =(TextView)view.findViewById(R.id.tv_total_price);
                    holderHost.r_loss =(TextView)view.findViewById(R.id.tv_loss_money);
                    holderHost.r_departTime =(TextView)view.findViewById(R.id.tv_depart_time);
                    holderHost.r_button1 =(TextView)view.findViewById(R.id.button1);
                    holderHost.r_button2 =(TextView)view.findViewById(R.id.button2);
                    holderHost.r_text =(TextView)view.findViewById(R.id.text);

                    holderHost.r_line =(View)view.findViewById(R.id.line);
                    holderHost.r_title_info =(TextView)view.findViewById(R.id.tv_title_info);

                    holderHost.r_ll_loss =(LinearLayout)view.findViewById(R.id.ll_loss);
                    holderHost.r_button3 =(TextView)view.findViewById(R.id.button3);
                }
                view.setTag(holderHost);
            }else {
                holderHost= (ViewHolderHost) view.getTag();
            }
            if (getItemViewType(i)==3){//车主待付款
                holderHost.wp_title_money.setText("爽约金"+order.getLoss_money()+"元");
                holderHost.wp_total_price.setText("");
                holderHost.wp_start_pos.setText(order.getStart_place());
                holderHost.wp_end_pos.setText(order.getEnd_place());
                holderHost.wp_seats.setText(order.getSeats()+"个");
                holderHost.wp_total_price.setText(order.getTotal_money()+"元");
                holderHost.wp_loss.setText(order.getLoss_money()+"元");
                holderHost.wp_departTime.setText(order.getDepart_time());
                if (order.getIs_timeout()==Constant.YES){
                    holderHost.wp_title_info.setText("已超时，需支付");
                    if (order.getForm_way()==Constant.RELEASE){
                        holderHost.wp_btn_release.setText("重新发布");
                        holderHost.wp_tv_words_release.setText("我的留言："+order.getWords());
                        holderHost.wp_ll_owner_info.setVisibility(View.GONE);
                        holderHost.wp_line.setVisibility(View.GONE);
                        holderHost.wp_tv_words_release.setVisibility(View.VISIBLE);
                    }else {
                        holderHost.wp_btn_release.setVisibility(View.GONE);
                        holderHost.wp_tv_words_release.setVisibility(View.GONE);
                        holderHost.wp_line.setVisibility(View.VISIBLE);
                        holderHost.wp_ll_owner_info.setVisibility(View.VISIBLE);
                        Picasso.with(context).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holderHost.wp_icon);
                        holderHost.wp_nickname.setText(order.getNickname());
                        holderHost.wp_recent_come.setText("[24h前来过]");// TODO: 2017/11/13 喂一下相关
                        if (TextUtils.isEmpty(order.getWords())){
                            holderHost.wp_words.setText("乘客留言：乘客很懒，没有留言");
                        }else {
                            holderHost.wp_words.setText("乘客留言："+order.getWords());
                        }
                    }
                }else {
                    if (order.getForm_way()==Constant.RELEASE){//信息未发布
                        holderHost.wp_line.setVisibility(View.GONE);
                        holderHost.wp_tv_words_release.setVisibility(View.VISIBLE);
                        holderHost.wp_ll_owner_info.setVisibility(View.GONE);
                        holderHost.wp_btn_release.setText("立即发布");
                        holderHost.wp_title_info.setText("信息未发布，需支付");
                        holderHost.wp_tv_words_release.setText("我的留言："+order.getWords());
                        Log.d("wp_title_info",holderHost.wp_title_info.getText().toString()+holderHost.wp_btn_release.getText().toString());

                    }else if (order.getForm_way()==Constant.BUY){//未购票
                        holderHost.wp_line.setVisibility(View.VISIBLE);
                        holderHost.wp_tv_words_release.setVisibility(View.GONE);
                        holderHost.wp_ll_owner_info.setVisibility(View.VISIBLE);
                        holderHost.wp_title_info.setText("抢单未成功，需支付");
                        holderHost.wp_btn_release.setText("立即抢单");
                        Picasso.with(context).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holderHost.wp_icon);
                        holderHost.wp_nickname.setText(order.getNickname());
                        holderHost.wp_recent_come.setText("[24h前来过]");// TODO: 2017/11/13 喂一下相关
                        if (TextUtils.isEmpty(order.getWords())){
                            holderHost.wp_words.setText("乘客留言：乘客很懒，没有留言");
                        }else {
                            holderHost.wp_words.setText("乘客留言："+order.getWords());
                        }
                    }
                }


                holderHost.wp_btn_release.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (order.getIs_timeout()==Constant.YES){
                            //Toast.makeText(context,"重新发布"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            if (order.getForm_way()==Constant.RELEASE){
                                Intent intent = new Intent(context, ReleaseActivity.class);
                                intent.putExtra("identity",order.getIdentity());
                                context.startActivity(intent);
                            }else {

                            }
                        }else {
                            if (order.getForm_way()==Constant.RELEASE){
                                //Toast.makeText(context,"立即发布"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                //调用车主发布生成订单接口，跳转支付页面
                                onOrderHandleListener.onReleaseNow(order,i);

                            }else {
                                //Toast.makeText(context,"立即抢单"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                //调用车主抢单生成订单接口，跳转支付页面
                                Intent intent = new Intent(context, BookPayActivity.class);
                                intent.putExtra("orderCode",order.getOrder_id());
                                intent.putExtra("amount", order.getLoss_money()+"");
                                intent.putExtra("from", Constant.HOST_ROB_PAY);
                                intent.putExtra("id",order.getService_id());
                                intent.putExtra("alipay",Common.HOST_SEARCH_ALIPAY);
                                intent.putExtra("yivipay",Common.HOST_SEARCH_RESTPAY);
                                context.startActivity(intent);
                            }
                        }

                    }
                });
            }else if (getItemViewType(i)==4){//车主待接单
                holderHost.wt_title_info.setTextColor(context.getResources().getColor(R.color.color_offline));
                holderHost.wt_title_info.setText("剩余票数"+order.getRest_tickets()+"张");
                holderHost.wt_seats.setText(order.getSeats()+"个");
                holderHost.wt_words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chezhu),null,null,null);
                // TODO: 2017/11/16
                if (TextUtils.isEmpty(order.getWords())){
                    holderHost.wt_words.setText("我的留言：没有留言");

                }else {
                    holderHost.wt_words.setText("我的留言："+order.getWords());

                }
                holderHost.wt_start_pos.setText(order.getStart_place());
                holderHost.wt_end_pos.setText(order.getEnd_place());
                holderHost.wt_price.setText(order.getSingle_money()+"元");
                holderHost.wt_total_price.setText(order.getTotal_money()+"元");
                holderHost.wt_loss.setText(order.getLoss_money()+"元");
                holderHost.wt_departTime.setText(order.getDepart_time());
                if (order.getIs_timeout()==Constant.YES){
                    holderHost.wt_title_info.setText("已超时，剩余票数"+order.getRest_tickets()+"张");
                    holderHost.wt_button1.setText("取消订单");
                    holderHost.wt_button2.setText("再次发布");
                }else{
                    holderHost.wt_title_info.setText("剩余票数"+order.getRest_tickets()+"张");
                    if (order.getIs_sale_start()==Constant.YES){
                        holderHost.wt_button1.setText("停止售票");
                        holderHost.wt_button2.setText("取消订单");
                    }else {
                        holderHost.wt_button1.setText("开始售票");
                        holderHost.wt_button2.setText("取消订单");
                    }

                }
                holderHost.wt_button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (order.getIs_timeout()==Constant.YES){
                            //Toast.makeText(context,"取消订单"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            //退未售出票数爽约金，判定服务无效
                            onOrderHandleListener.onCancelOrder(order.getForm_way()+"",order.getOrder_type()+"",order.getOrder_id()+"",i,Constant.CANCEL_ORDER);

                        }else {
                            if (order.getIs_sale_start()==Constant.YES){
                                onOrderHandleListener.onEndSellTicket(order.getService_id(),order.getIs_sale_start()+"",Constant.END_SELL,i);
                                //Toast.makeText(context,"停止售票"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            }else {
                                onOrderHandleListener.onStartSellTicket(order.getService_id(),order.getIs_sale_start()+"",Constant.START_SELL,i);
                                //Toast.makeText(context,"开始售票"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            }
                            //调用接口，成功之后改变按钮状态，即是否对乘客可见，乘客购买座位之后，待服务中多出一条订单
                        }

                    }
                });
                holderHost.wt_button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (order.getIs_timeout()==Constant.YES){
                           // Toast.makeText(context,"再次发布"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                            //退未售出票数的爽约金，跳转重新发布页面
                            Intent intent = new Intent(context, ReleaseActivity.class);
                            intent.putExtra("identity",order.getIdentity());
                            context.startActivity(intent);
                        }else {
                            //退未售出票数爽约金，判定服务无效
                        }

                    }
                });
            }else {//车主剩余
                Picasso.with(context).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holderHost.r_icon);
                if(TextUtils.isEmpty(order.getNickname())){
                    holderHost.r_nickname.setText("暂无昵称");
                }else {
                    holderHost.r_nickname.setText(order.getNickname());
                }
                holderHost.r_recent_come.setText("[24h前来过]");// TODO: 2017/11/14 喂一下

                if (order.getWho_created()==Constant.HOST){
                    holderHost.r_words.setText("乘客留言："+order.getWords());
                    holderHost.r_words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chengke),null,null,null);
                    holderHost.r_text.setText("座位需求");
                }else {
                    holderHost.r_words.setText("我的留言："+order.getWords());
                    holderHost.r_words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chezhu),null,null,null);
                    holderHost.r_text.setText("提供座位");
                }

                holderHost.r_start_pos.setText(order.getStart_place());
                holderHost.r_end_pos.setText(order.getEnd_place());
                holderHost.r_seats.setText(order.getSeats()+"个");
                holderHost.r_total_price.setText(order.getTotal_money()+"元");
                holderHost.r_departTime.setText(order.getDepart_time());
                if (order.getOrder_type()==Constant.WAIT_SURVICE){
                    holderHost.r_ll_loss.setVisibility(View.VISIBLE);
                    holderHost.r_line.setVisibility(View.GONE);
                    holderHost.r_title_info.setVisibility(View.GONE);
                    holderHost.r_loss.setText(order.getLoss_money()+"元");
                    holderHost.r_button1.setText("开始服务");

                }else if (order.getOrder_type()==Constant.IN_SURVICE){
                    holderHost.r_ll_loss.setVisibility(View.GONE);
                    holderHost.r_line.setVisibility(View.GONE);
                    holderHost.r_title_info.setVisibility(View.GONE);
                    holderHost.r_button1.setText("行程完结");
                }else if (order.getOrder_type()==Constant.WAIT_FINISH){
                    holderHost.r_ll_loss.setVisibility(View.GONE);
                    if (order.getIs_finished()==Constant.HOST_FINISHED_PASSENGER_NO){//等待对方完结
                        holderHost.r_button1.setVisibility(View.GONE);
                        holderHost.r_line.setVisibility(View.VISIBLE);
                        holderHost.r_title_info.setVisibility(View.VISIBLE);
                        holderHost.r_title_info.setText("等待对方完结");

                    }else if (order.getIs_finished()==Constant.PASSENGER_FINISHED_HOST_NO){
                        holderHost.r_button1.setVisibility(View.VISIBLE);
                        holderHost.r_button1.setText("行程完结");
                        holderHost.r_line.setVisibility(View.GONE);
                        holderHost.r_title_info.setVisibility(View.GONE);
                    }

                }else if (order.getOrder_type()==Constant.FINISHED){
                    holderHost.r_ll_loss.setVisibility(View.GONE);
                    holderHost.r_button1.setVisibility(View.VISIBLE);
                    holderHost.r_button1.setText("再次发布");
                    holderHost.r_line.setVisibility(View.GONE);
                    holderHost.r_title_info.setVisibility(View.GONE);
                }


                holderHost.r_button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (order.getOrder_type()){
                            case Constant.WAIT_SURVICE:
                                onOrderHandleListener.onStartService(order.getOrder_id(),Constant.START_SERVICE,i);
                                break;
                            case Constant.IN_SURVICE:
                                // Toast.makeText(context,"服务中行程完结"+"身份"+identity+"订单类型"+order_type+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                onOrderHandleListener.onRouteFinish(order.getIdentity()+"",order.getOrder_id(),i,Constant.ROUTE_FINISHED);

                                break;
                            case Constant.WAIT_FINISH:
                                // Toast.makeText(context,"待完结行程完结"+"身份"+identity+"订单类型"+order_type+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                onOrderHandleListener.onRouteFinish(order.getIdentity()+"",order.getOrder_id(),i,Constant.ROUTE_FINISHED);

                                break;
                            case Constant.FINISHED:
                                //Toast.makeText(context,"再次发布"+"身份"+identity+"订单类型"+order.getOrder_type()+"出行方式"+go_type+"第"+i+"条",Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(context, ReleaseActivity.class);
                                intent.putExtra("identity",order.getIdentity());
                                context.startActivity(intent);
                                break;
                        }

                    }
                });
                holderHost.r_button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(order.getPhone())){
                            CallPhone.callPhone(context,order.getPhone());
                        }else {
                            Toast.makeText(context,"电话为空",Toast.LENGTH_LONG).show();
                        }
                    }
                });
                holderHost.r_button3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context,"暂未开放",Toast.LENGTH_LONG).show();

                    }
                });

            }
        }

        return view;
    }

    /*
    * 乘客长途
    * */
    private class ViewHolder{//乘客，长途，待付款
        TextView wp_title_info;
        TextView wp_title_money;
        CircleImage wp_icon;
        TextView wp_nickname;
        TextView wp_time;
        TextView wp_words;
        TextView wp_start_pos;
        TextView wp_end_pos;
        TextView wp_seats;
        TextView wp_total_price;
        TextView wp_departTime;
        TextView wp_btnBuy;
        View wp_line;
        TextView wp_tv_words_release;
        LinearLayout wp_ll_buy;
        LinearLayout wp_ll_bottom_release;
        LinearLayout wp_ll_bottom_buy;
        TextView wp_release_again;
        //待接单
        TextView wt_title_info;
        TextView wt_words;
        TextView wt_start_pos;
        TextView wt_end_pos;
        TextView wt_seats;
        TextView wt_total_price;
        TextView wt_departTime;
        TextView wt_btnCancel;
        TextView wt_btnReleaseAgain;

        //乘客剩余
        CircleImage r_icon;
        TextView r_nickname;
        TextView r_recent_come;
        TextView r_words;
        TextView r_start_pos;
        TextView r_end_pos;
        TextView r_seats;
        TextView r_total_price;
        TextView r_loss;
        TextView r_departTime;
        TextView r_btnPhone;
        TextView r_btnWei;
        TextView r_btnInCar;
        TextView r_text;
        LinearLayout r_ll_loss;
        TextView r_title_info;
        View r_line;


    }


    /*
    * 车主长途
    * */
    private class ViewHolderHost{//车主，长途，待付款
        TextView wp_title_info;
        TextView wp_title_money;
        CircleImage wp_icon;
        TextView wp_nickname;
        TextView wp_recent_come;
        TextView wp_words;
        TextView wp_start_pos;
        TextView wp_end_pos;
        TextView wp_seats;
        TextView wp_total_price;
        TextView wp_departTime;
        View wp_line;
        TextView wp_tv_words_release;
        LinearLayout wp_ll_owner_info;
        TextView wp_btn_release;
        TextView wp_loss;

        //待接单
        TextView wt_title_info;
        TextView wt_words;
        TextView wt_start_pos;
        TextView wt_end_pos;
        TextView wt_seats;
        TextView wt_total_price;
        TextView wt_price;
        TextView wt_loss;
        TextView wt_departTime;
        TextView wt_button1;
        TextView wt_button2;
        //车主剩余
        CircleImage r_icon;
        TextView r_nickname;
        TextView r_recent_come;
        TextView r_words;
        TextView r_start_pos;
        TextView r_end_pos;
        TextView r_seats;
        TextView r_total_price;
        TextView r_loss;
        TextView r_departTime;
        TextView r_button1;
        TextView r_button2;
        TextView r_button3;
        TextView r_text;
        LinearLayout r_ll_loss;
        TextView r_title_info;
        View r_line;

    }

    public interface OnOrderHandleListener {
        void onStartService(String order_id,String from,int position);
        void onStartSellTicket(String service_id,String isPermit,String from,int position);
        void onEndSellTicket(String service_id,String isPermit,String from,int position);
        void onCancelOrder(String type,String serviceStatus,String orderId,int position,String from);
        void onRouteFinish(String role,String orderId,int position,String from);
        void onAlreadyInCarClick(String orderId,int position,String from);
        void onReleaseNow(Order order , int position);
    }
    public void setOnOrderHandleListener(OrderInProcessAdapter.OnOrderHandleListener onOrderHandleListener){
        this.onOrderHandleListener = onOrderHandleListener;
    }
}
